// Total number of calls
var callNum = 0;

function checkWinner(form,caller,winningNum){
  // Increment the call number
  ++callNum;
  
  // Check for a winner
  if (callNum == winningNum) {
    alert(caller+", caller number "+callNum+" ... today's winner!");
    alert("form submit");
  } else {
    // Reset the caller field for the next caller
    var callerField = document.getElementById("caller");
    callerField.value = "Next caller";
    callerField.focus();
    callerField.select();
  }
}

function DebugConsole(){
  // Create the debug console area
  var consoleElem = document.createElement("div");
  consoleElem.id = "debug";
  consoleElem.style.fontFamily = "monospace";
  consoleElem.style.color = "#333333";

  document.body.appendChild(consoleElem);
  consoleElem.appendChild(document.createElement("hr"));
  //  Create the alternating background color property
  this.shaded = false;
}

DebugConsole.prototype.displayMsg = function(msg){
  //  Create the message
  var msgElement = document.createElement("div");
  msgElement.appendChild(document.createTextNode(msg));
  msgElement.style.backgroundColor = this.shaded? "#EEEEEE":"#FFFFFF";
  var consoleElem = document.getElementById("debug");
  consoleElem.appendChild(msgElement);
  // Toggle the alternating background color property
  this.shaded = !this.shaded;
}


window.onload = function(){
  document.getElementById("submitbutton").onclick = function(){
    checkWinner(document.getElementById("callform"),document.getElementById("caller").value, 7);
    console.displayMsg("callNum: " + callNum);
  }
  console = new DebugConsole();
}
